package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public PageResult<Role> list(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Role> roleList = roleMapper.selectList(name, label);

        Page<Role> p = (Page<Role>) roleList;

        return new PageResult<>(p.getTotal(), p.getResult());

    }

    @Override
    public void delete(Integer id) {
        roleMapper.delete(id);
    }

    @Override
    public void add(Role role) {
        //补全创建时间、更新时间
        role.setCreateTime(LocalDateTime.now());
        role. setUpdateTime(LocalDateTime.now());

        roleMapper.insert(role);

    }

    @Override
    public void update(Role role) {
        //补充更新时间
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);

    }

    @Override
    public Role getById(Integer id) {
        return roleMapper.selectByid(id);


    }

    @Override
    public List<Role> findList() {
        return roleMapper.listAll();
    }

}
